package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.config.R;
import com.example.entity.Category;
import com.example.entity.Employee;
import com.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @RequestMapping("/page")
    public R page(Integer page, Integer pageSize, String name){
        IPage<Category> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        if (name!=null){
            queryWrapper.like(Category::getName, name);
        }
        //queryWrapper.orderByDesc(Employee::getUpdateTime);
        //loginService.page(pageInfo,queryWrapper);
        IPage<Category> iPage = categoryService.page(pageInfo, queryWrapper);
        return R.success(iPage);
    }

    @PostMapping
    public R addCategory(@RequestBody Category category, HttpSession session){
        Long id=(Long)session.getAttribute("employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);
        categoryService.add(category);
        return R.success("成功");
    }
}
